import {Card} from 'antd';
import React, {useEffect, useRef, useState} from 'react';
import {PageContainer} from '@ant-design/pro-layout';
import ProTable, {ActionType} from '@ant-design/pro-table';
import type {TableListItem} from './data.d';
import {queryLiquidityList} from './service';
import {columnsSorter, columnToColumnsState, empty, formatDate, NumberDiv} from "@/utils/function";
import {queryUse} from "@/utils/service";
import {getSelectList} from "@/pages/BTCList/getSelectList";

const LiquidityList: React.FC = () => {
  let columns: any[] = [
    {
      title: "流动资金池",
      dataIndex: 'poolName',
      key: 'poolName',
      fixed: "left",
      width: 90,
      tip: '流动资金池是唯一的 key',
    },
    {
      title: "买入份额",
      width: 120,
      dataIndex: 'shareAmount',
      key: 'shareAmount',
      renderText: (val: string | number) => NumberDiv(val,1,4)
    },
    {
      title: "流动资金池资产份额",
      width: 120,
      dataIndex: 'asset',
      key: 'asset',
      renderText: (val: string | number) => NumberDiv(val,1,4)
    },
    {
      title: "当前资产份额",
      width: 120,
      dataIndex: 'currentAsset',
      key: 'currentAsset',
      renderText: (val: string | number) => NumberDiv(val,1,4)
    },
    {
      title: "浮动盈亏",
      width: 120,
      dataIndex: 'updateTime',
      key: 'updateTime',
      renderText: (val: string) => formatDate(val, false)
    },
  ];
  columnsSorter("LiquidityList流动挖矿收益统计", columns);
  const [col, setCol] = useState<any>(columnToColumnsState(columns));
  const [dataTable, setDataTable] = useState<any>(null);
  const actionRef = useRef<ActionType>();
  useEffect(() => {
    getSelectList("4","LiquidityList", actionRef).then(
      (selectList: React.ReactNode)=>{
        setDataTable(<ProTable<TableListItem>
          headerTitle={""}
          rowKey="key"
          bordered
          actionRef={actionRef}
          columnsStateMap={col}
          onColumnsStateChange={(e) => {
            if (!empty(e)) {
              localStorage.setItem("LiquidityList流动挖矿收益统计", JSON.stringify(e));
              setCol(e);
            }
          }}
          toolBarRender={() => [
            <div>{selectList}</div>,
          ]}
          pagination={false}
          scroll={{ y: 666}}
          search={false}
          request={async (params = {}, sort, filter) => {
            //console.log(sort, filter);
            const msg = await queryLiquidityList();
            let data = queryUse(msg)
            if (data) {
              return {
                data: data,
                // success 请返回 true，
                // 不然 table 会停止解析数据，即使有数据
                success: true,
                // 不传会使用 data 的长度，如果是分页一定要传
                // total: number,
              };
            } else {
              return {
                data: data,
                success: false,
              };
            }
          }}
          columns={columns}
        />)
      });
  }, [col]);
  //处理同步数据

  return (
    <PageContainer>
      <Card>
        {dataTable}
      </Card>
    </PageContainer>
  );
};

export default LiquidityList;





